import axios from 'axios'
import {
  Toast
} from 'vant'

axios.defaults.baseURL = 'http://127.0.0.1:3000'

axios.defaults.withCredentials = true // 让ajax携带cookie

axios.interceptors.response.use(
  res => {
    if (res.data.error === 400) {
      Toast.fail(res.data.message)
      // console.log(location)

      location.href = `/#/mobile/login?repath=/${location.hash}`
    }
    return res
  })

export const mobile = {
  // 一级分类查询
  queryTopCategory: () => axios.get('/category/queryTopCategory'),

  // 二级分类查询
  querySecCategory: obj => axios.get('/category/querySecondCategory', {
    params: obj
  }),

  // 登录
  login: obj => axios.post('/user/login', obj),

  // 获取用户信息
  queryUseMsg: () => axios.get('/user/queryUserMessage'),

  // 查询购物车
  queryCart: () => axios.get('/cart/queryCart'),

  // 搜索查询
  queryProduct: obj => axios.get('/product/queryProduct', {
    params: obj
  }),
  // 商品详情查询
  queryProductDetail: obj => axios.get('/product/queryProductDetail', {
    params: obj
  })
}
